//
//  LJFRightController.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/15.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFRightController.h"
#import "LJFRightModel.h"
#import "LJFContinentModel.h"
#import "LJFCountryModel.h"
#import "LJFCityModel.h"
#import "LJFRigthCell.h"
#import "Masonry.h"
#import "LJFSearchResultController.h"


@interface LJFRightController ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *_tableView;
    
    NSMutableArray *_dataArr;
    
    NSMutableArray *_tableData;
}

@end

@implementation LJFRightController

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self)
    {
        _dataArr = [[NSMutableArray alloc] init];
        _tableData = [[NSMutableArray alloc] init];

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //self.view.backgroundColor = [UIColor greenColor];
    
    [self createTableView];
    
    [self addMyObserver];
    
   // [self loadTableDataWithIndex:0];

}

#pragma mark - 创建tableview

- (void)createTableView
{
    _tableView = [[UITableView alloc] init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.view.mas_top).offset(0);
         make.left.equalTo(self.view.mas_left).offset(0);
         make.bottom.equalTo(self.view.mas_bottom).offset(0);
         make.right.equalTo(self.view.mas_right).offset(0);
     }];
    
    [_tableView registerNib:[UINib nibWithNibName:@"LJFRigthCell" bundle:nil] forCellReuseIdentifier:@"LJFRigthCell"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 添加观察者

- (void)addMyObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveData:) name:@"SevenZhou" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableView:) name:@"number" object:nil];
}

#pragma mark - 观察者实现方法

- (void)receiveData:(NSNotification *)info
{
    NSArray *arr = info.object;
    
    [_dataArr addObjectsFromArray:arr];
    
    [self loadTableDataWithIndex:0];

}

- (void)reloadTableView:(NSNotification *)info
{
    NSNumber *num = info.object;
    
    int index = [num intValue];
    
    [_tableData removeAllObjects];
    
    [self loadTableDataWithIndex:index];
    
    [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

#pragma mark - 控制器销毁时 销毁观察者

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 加载数据源

- (void)loadTableDataWithIndex:(int)index
{
    if (index >= _dataArr.count)
    {
        return;
    }
    
    LJFContinentModel *ContModel = _dataArr[index];
    
    for (LJFCountryModel *CModel in ContModel.children)
    {
        LJFRightModel *model = [[LJFRightModel alloc] init];
        
        model.name = CModel.name;
        model.myid = CModel.Myid;
        
        NSMutableArray *arrM = [[NSMutableArray alloc] init];
        for (LJFCityModel *cityModel in CModel.children)
        {
            NSString *str = cityModel.name;
            
            [arrM addObject:str];
        }
        
        NSString *titleStr = [arrM componentsJoinedByString:@","];
        
        model.mytitle = titleStr;
        
        [_tableData addObject:model];
    }
    
    [_tableView reloadData];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -UITableViewDelegate, UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableData.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJFRightModel *model = _tableData[indexPath.row];
   
    LJFRigthCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LJFRigthCell"];
    cell.title.text = model.name;
    cell.subtitle.text = model.mytitle;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJFRightModel *model = _tableData[indexPath.row];
    
    LJFSearchResultController *searchResult = [self.storyboard instantiateViewControllerWithIdentifier:@"LJFSearchResultController"];
    searchResult.dest_id = model.myid;
    searchResult.sort_by = @"top_sale";
    
    [self.navigationController pushViewController:searchResult animated:YES];
}

@end
